package com.hepinghospital.user.ui.activity.home;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.shape.view.ShapeTextView;
import com.kingja.loadsir.core.LoadSir;
import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.util.ImageLoaderHelper;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.CloudDoctorResponse;
import com.rest.response.Doctor;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import jz.joyoung.robot.util.UtilString;

/**
 * 门诊预约——医生列表
 *
 * @author HP
 * @date 2020/2/20
 */

public class BookDailyDoctorByDateActivity extends BaseActivity {

    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    @BindView(R.id.doctor_recyclerView)
    RecyclerView doctor_recyclerView;
    @BindView(R.id.date_recyclerView)
    RecyclerView date_recyclerView;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    List<DateTime> dateList = new ArrayList<>();
    List<Doctor> doctorList = new ArrayList<>();


    DoctorAdapter doctorAdapter;
    DateAdapter dateAdapter;

    int current = 1;
    String currentString = "", currentId = "";
    String deptId = "0", codeType = "", code = "";
    private String isPrescription = "";


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_book_daily_docot_date;
    }

    @Override
    protected void initViews() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        doctor_recyclerView.setLayoutManager(manager);
        doctorAdapter = new DoctorAdapter();
        doctor_recyclerView.setAdapter(doctorAdapter);

        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> getDoctorList(currentId, currentString, "", "", isPrescription, code));
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {
            current++;
            getDoctorList(currentId, currentString, "", "", isPrescription, code);
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(BookDailyDoctorByDateActivity.this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        date_recyclerView.setLayoutManager(linearLayoutManager);
        dateAdapter = new DateAdapter();
        date_recyclerView.setAdapter(dateAdapter);
    }

    @Override
    protected void initData() {
        current = 1;
        currentId = getIntent().getStringExtra("id");
        tvTitle.setText(getIntent().getStringExtra("depName"));
        getDoctorList(currentId, currentString, "", "", isPrescription, code);
        getDateList();
    }

    class DoctorAdapter extends RecyclerView.Adapter<DoctorAdapter.Holder> {

        @NotNull
        @Override
        public Holder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder(LayoutInflater.from(BookDailyDoctorByDateActivity.this).inflate(R.layout.item_book_doctor_online, parent, false));
            return myHolder;
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.itemView.setOnClickListener(v -> {
                Bundle bundleCloud = new Bundle();
                bundleCloud.putString("id", doctorList.get(position).id);
                startActivity(DoctorDetailActivity.class, bundleCloud);
            });
            holder.tv_level.setText(doctorList.get(position).jobTitleName);
            holder.tv_skill.setText("擅长:" + doctorList.get(position).docGoodAtNames);
            holder.tv_star.setText(doctorList.get(position).score);
            holder.tv_name.setText(doctorList.get(position).docName);
            ImageLoaderHelper.getInstance().GlideCircleImageLoader(BookDailyDoctorByDateActivity.this, doctorList.get(position).thumbnailUrl, holder.img_head);
            if (doctorList.get(position).isPrescription == 1) {
                holder.tv_recipel.setVisibility(View.VISIBLE);
//                holder.tv_recipel.setText("处方");
//                holder.tv_recipel.setTextColor(Color.parseColor("#ff209187"));
            } else {
                holder.tv_recipel.setVisibility(View.GONE);
//                holder.tv_recipel.setText("非处方");
//                holder.tv_recipel.setTextColor(Color.parseColor("#ffd5d5d5"));
            }
            if (!UtilString.isEmpty(doctorList.get(position).num)) {
                holder.tv_turnNum.setVisibility(View.VISIBLE);
                holder.tv_turnNum.setText(doctorList.get(position).num);
            } else {
                holder.tv_turnNum.setVisibility(View.GONE);
            }
            if (!UtilString.isEmpty(doctorList.get(position).deptName)) {
                holder.tv_deptName.setText(doctorList.get(position).deptName);
            } else {
                holder.tv_deptName.setVisibility(View.GONE);
            }
            holder.tvRecommend.setVisibility(View.GONE);
//            if ("0".equals(doctorList.get(position).isRecommend)) {
//                holder.tvRecommend.setVisibility(View.GONE);
//            } else {
//                holder.tvRecommend.setVisibility(View.VISIBLE);
//            }
            if (doctorList.get(position).diagType == 0) {
                holder.btnCloud.setVisibility(View.VISIBLE);
                holder.btnGraph.setVisibility(View.VISIBLE);
            } else if (doctorList.get(position).diagType == 1) {
                holder.btnGraph.setVisibility(View.VISIBLE);
                holder.btnCloud.setVisibility(View.GONE);
            } else if (doctorList.get(position).diagType == 3) {
                holder.btnGraph.setVisibility(View.GONE);
                holder.btnCloud.setVisibility(View.VISIBLE);
            } else {
                holder.btnGraph.setVisibility(View.GONE);
                holder.btnCloud.setVisibility(View.GONE);
            }
//            holder.ratingBar.setRating(Float.parseFloat(doctorList.get(position).score));
        }

        @Override
        public int getItemCount() {
            return doctorList.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name, tv_level, tv_recipel, tv_deptName, tv_skill, tv_star, tv_turnNum, tvRecommend;
            ImageView img_head;
            ShapeTextView btnCloud, btnGraph;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_level = itemView.findViewById(R.id.tv_level);
                tv_recipel = itemView.findViewById(R.id.tv_recipel);
                tv_deptName = itemView.findViewById(R.id.tv_deptName);
                tv_skill = itemView.findViewById(R.id.tv_skill);
                tv_star = itemView.findViewById(R.id.tv_star);
                img_head = itemView.findViewById(R.id.img_head);
//                ratingBar = itemView.findViewById(R.id.ratingBar);
                tv_turnNum = itemView.findViewById(R.id.tv_turn_num);
                tvRecommend = itemView.findViewById(R.id.iv_recommend);
                btnCloud = itemView.findViewById(R.id.btn_cloud);
                btnGraph = itemView.findViewById(R.id.btn_graph);
            }
        }
    }


    class DateAdapter extends RecyclerView.Adapter<DateAdapter.Holder> {

        @NotNull
        @Override
        public Holder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder(LayoutInflater.from(BookDailyDoctorByDateActivity.this).inflate(R.layout.item_book_date, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(@NotNull Holder holder, final int position) {
            if (dateList.get(position).isSelect) {
                holder.tv_date.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                holder.tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                holder.tv_all.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                holder.container.setBackgroundResource(R.drawable.item_bg_green);
            } else {
                holder.tv_date.setTextColor(ContextCompat.getColor(mContext, R.color.lightBlack));
                holder.tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.lightBlack));
                holder.tv_all.setTextColor(ContextCompat.getColor(mContext, R.color.lightBlack));
                holder.container.setBackgroundResource(R.drawable.item_bg_grey);
            }
            if (position == 0) {
                holder.tv_date.setVisibility(View.INVISIBLE);
                holder.tv_time.setVisibility(View.INVISIBLE);
                holder.tv_all.setVisibility(View.VISIBLE);
            } else {
                holder.tv_date.setVisibility(View.VISIBLE);
                holder.tv_time.setVisibility(View.VISIBLE);
                holder.tv_all.setVisibility(View.INVISIBLE);
                holder.tv_date.setText(dateList.get(position).date);
                holder.tv_time.setText(dateList.get(position).time);
            }
            holder.itemView.setOnClickListener(v -> {
                if (position == 0) {
                    currentString = "";
                    getDoctorList(currentId, currentString, "", "", isPrescription, code);
                } else {
                    getDoctorList(currentId, currentString, dateList.get(position).allDate, dateList.get(position).timeId, isPrescription, code);
                }
                for (int i = 0; i < dateList.size(); i++) {
                    dateList.get(i).isSelect = false;
                }
                dateList.get(position).isSelect = true;
                notifyDataSetChanged();
            });
        }

        @Override
        public int getItemCount() {
            return dateList.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_date, tv_time, tv_all;
            RelativeLayout container;

            public Holder(View itemView) {
                super(itemView);
                tv_date = itemView.findViewById(R.id.tv_date);
                tv_time = itemView.findViewById(R.id.tv_time);
                tv_all = itemView.findViewById(R.id.tv_all);
                container = itemView.findViewById(R.id.container);
            }
        }
    }


    private void getDateList() {
        dateList.add(new DateTime());
        for (int i = 1; i < 15; i++) {
            for (int j = 0; j < 2; j++) {
                Calendar c = Calendar.getInstance();
                DateTime dateTime = new DateTime();
                int day1 = c.get(Calendar.DATE);
                c.set(Calendar.DATE, day1 + i);
                dateTime.date = getTime(c.getTime());
                dateTime.allDate = getAllTime(c.getTime());
                if (j == 0) {
                    dateTime.time = getDay(c) + "上午";
                    dateTime.timeId = "am";
                } else {
                    dateTime.time = getDay(c) + "下午";
                    dateTime.timeId = "pm";
                }
                dateList.add(dateTime);
            }
        }
        dateList.get(0).isSelect = true;
    }


    private void getDoctorList(String id, String content, String date, String time, String isPrescription, String code) {
        RestProxy.getInstance().getCloudDoctorListIsPrescription(id, content, date, time, "4", current + "", "10", isPrescription, code, new BaseObserver<CloudDoctorResponse>() {
            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(BookDailyDoctorByDateActivity.this, e);
                refreshLayout.finishRefresh(false);
            }


            @Override
            public void onSuccess(CloudDoctorResponse doctorResponse) {
                doctorList.clear();
                doctorList.addAll(doctorResponse.records);
                doctorAdapter.notifyDataSetChanged();
                refreshLayout.finishRefresh(true);
            }
        });
    }

    static class DateTime {
        public String date;
        public String time;
        String timeId;
        String allDate;
        boolean isSelect = false;
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        @SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("MM.dd");
        return format.format(date);
    }

    private String getAllTime(Date date) {//可根据需要自行截取数据显示
        @SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    private String getDay(Calendar c) {
        String[] arr = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        return arr[c.get(Calendar.DAY_OF_WEEK) - 1];
    }
}
